package com.quanyan.place.thread;

import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlan;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlanExample;
import com.quanyan.place.entity.TbBizPlaceUnitSalePlanMain;
import com.quanyan.place.mapper.TbBizPlaceUnitSalePlanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by youxiaojia on 2016/10/13.
 */
public class ModifyPlaceSalePlanBundleThread implements Runnable{
    private Logger logger = LoggerFactory.getLogger(ModifyPlaceSalePlanBundleThread.class);

    private List<TbBizPlaceUnitSalePlanMain> tbBizPlaceUnitSalePlanMainList;

    private TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper;

    private Integer flag;//设置捆绑，取消捆绑标识

    public ModifyPlaceSalePlanBundleThread(List<TbBizPlaceUnitSalePlanMain> tbBizPlaceUnitSalePlanMainList, TbBizPlaceUnitSalePlanMapper tbBizPlaceUnitSalePlanMapper,Integer flag){
        this.tbBizPlaceUnitSalePlanMainList = tbBizPlaceUnitSalePlanMainList;
        this.tbBizPlaceUnitSalePlanMapper = tbBizPlaceUnitSalePlanMapper;
        this.flag = flag;
    }

    @Override
    public void run() {
        try {
            if (StringUtils.isEmpty(flag)){
                logger.debug("————————————————————————修改销售计划捆绑号开始——————————————————————");
                for (TbBizPlaceUnitSalePlanMain tbBizPlaceUnitSalePlanMain :tbBizPlaceUnitSalePlanMainList){
                    TbBizPlaceUnitSalePlanExample example = new TbBizPlaceUnitSalePlanExample();
                    TbBizPlaceUnitSalePlanExample.Criteria criteria = example.createCriteria();
                    criteria.andPlaceIdEqualTo(tbBizPlaceUnitSalePlanMain.getPlaceId());
                    criteria.andPlaceUnitIdEqualTo(tbBizPlaceUnitSalePlanMain.getPlaceUnitId());
                    criteria.andCategoryIdEqualTo(tbBizPlaceUnitSalePlanMain.getCategoryId());
                    criteria.andDateGreaterThanOrEqualTo(new Date());
                    criteria.andTimeTypeEqualTo(tbBizPlaceUnitSalePlanMain.getTimeType());
                    criteria.andStartTimeEqualTo(tbBizPlaceUnitSalePlanMain.getStartTime());
                    criteria.andEndTimeEqualTo(tbBizPlaceUnitSalePlanMain.getEndTime());
                    criteria.andIsEffectiveEqualTo(PlaceConstants.YES);
                    TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan = new TbBizPlaceUnitSalePlan();
                    String bundleNo = tbBizPlaceUnitSalePlanMain.getPlaceUnitId()+""+tbBizPlaceUnitSalePlanMain.getBundleNo();
                    tbBizPlaceUnitSalePlan.setBundleNo(Integer.valueOf(bundleNo));
                    tbBizPlaceUnitSalePlanMapper.updateByExampleSelective(tbBizPlaceUnitSalePlan,example);
                }
                logger.debug("————————————————————————修改销售计划捆绑号结束——————————————————————");
            }
            else if (0==flag.intValue()){
                logger.debug("————————————————————————取消销售计划捆绑号开始——————————————————————");
                for (TbBizPlaceUnitSalePlanMain tbBizPlaceUnitSalePlanMain :tbBizPlaceUnitSalePlanMainList){
                    TbBizPlaceUnitSalePlanExample example = new TbBizPlaceUnitSalePlanExample();
                    TbBizPlaceUnitSalePlanExample.Criteria criteria = example.createCriteria();
                    criteria.andPlaceIdEqualTo(tbBizPlaceUnitSalePlanMain.getPlaceId());
                    criteria.andPlaceUnitIdEqualTo(tbBizPlaceUnitSalePlanMain.getPlaceUnitId());
                    criteria.andCategoryIdEqualTo(tbBizPlaceUnitSalePlanMain.getCategoryId());
                    criteria.andDateGreaterThanOrEqualTo(new Date());
                    criteria.andTimeTypeEqualTo(tbBizPlaceUnitSalePlanMain.getTimeType());
                    criteria.andStartTimeEqualTo(tbBizPlaceUnitSalePlanMain.getStartTime());
                    criteria.andEndTimeEqualTo(tbBizPlaceUnitSalePlanMain.getEndTime());
                    criteria.andIsEffectiveEqualTo(PlaceConstants.YES);
                    TbBizPlaceUnitSalePlan tbBizPlaceUnitSalePlan = new TbBizPlaceUnitSalePlan();
                    tbBizPlaceUnitSalePlan.setBundleNo(0);
                    tbBizPlaceUnitSalePlanMapper.updateByExampleSelective(tbBizPlaceUnitSalePlan,example);
                }
                logger.debug("————————————————————————取消销售计划捆绑号结束——————————————————————");
            }

        } catch (Exception e) {
            logger.debug("————————————————————————更新销售计划捆绑号异常——————————————————————");
            logger.error(e.getMessage());
        }
    }
}
